//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    url: '',
    refresh: '0',
    userInfo: {}
  },
  onLoad: function (options) {
    if (options.f5) {
      this.setData({
        refresh: options.f5
      })
    }
    if (app.globalData.userInfo) {
      if (app.globalData.registerInfo) {
        this.urlAssignment()
      } else {
        app.accountCallback = res => {
          this.urlAssignment()
        }
        app.userLogin()
      }
    } else {
      wx.reLaunch({
        url: '/pages/authorize/index'
      })
    }
  },
  urlAssignment() {
    var that = this
    wx.showLoading({
      title: '页面加载中...',
    })
    wx.getStorage({
      key: 'addEmployee',
      fail (res) {
        that.setData({
          userInfo: app.globalData.userInfo,
          url: `${app.globalData.baseUrl}?userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
          encodeURI(JSON.stringify(app.globalData.registerInfo))}`
        })
      }
    })
    console.log(`${app.globalData.baseUrl}?userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
      encodeURI(JSON.stringify(app.globalData.registerInfo))}`)
    setTimeout(() => {
      wx.hideLoading()
    }, 2000)
  },
  h5Load: function () {
    wx.hideLoading()
  },
  h5LoadError: function (e) {
    wx.hideLoading()
    wx.showToast({
      title: e.detail,
      duration: 2000
    })
  },
  onShow: function () {
    var addEmployee = ''
    var that = this
    wx.getStorage({
      key: 'addEmployee',
      success (res) {
        addEmployee = res.data
        if (addEmployee.no == '1') {
          that.setData({
            url: ''
          })
          wx.removeStorage({
            key: "addEmployee"
          })
          setTimeout(function() {
            if (that.data.refresh == '1') {
              that.setData({
                url: `${app.globalData.baseUrl}?userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
                  encodeURI(JSON.stringify(app.globalData.registerInfo))}`,
                  refresh: '0'
              })
            } else {
              that.setData({
                url: `https://zhaq-sit-manager.bosafe.com/app/#/retrospect?type=${addEmployee.type}&userInfo=${encodeURI(JSON.stringify(app.globalData.userInfo))}&registerInfo=${
                  encodeURI(JSON.stringify(app.globalData.registerInfo))}`,
                  refresh: '0'
              })
            }
          }, 100)
        }
      }
    })
  }
})
